<?php
session_start();
include("classes/connect.php");
include('config.php');
require_once('config.php');
require_once('classes/class.jira.php');
ini_set('display_errors', 'Off');

$filename=$_GET['filename'];
$filename=html_entity_decode($filename);
$DBID=$_GET['dbid'];
$jiraid=$_GET['jiraid'];
$url="http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/Attachments/".$filename;
$deleteurl="Attachments/".$filename;
//deleting from the server
unlink($deleteurl);

//deleting from jira description
$config->username= $username;
$config->password= $pass;
$config->port= "";
$config->host= $host;

$query->id= $jiraid;
$queryIssue = new Jira($config);
$array=$queryIssue->queryIssue($query);
$response = json_decode($array->responseBody);
$reviseddes=str_replace("[".$url."]\n","",$response->issues[0]->fields->description);
$data->fields->description=$reviseddes;
$queryIssue->updateIssue($data,$jiraid);

//deleting from jira comments

$commentarray=$queryIssue->queryComment($query->id);
$commentresponse=json_decode($commentarray->responseBody);
$a=str_replace("[".$url."]\n","",$commentresponse->comments[0]->body);
$b=str_replace("\n","",$a);
if(strlen($b)==0){
$queryIssue->deleteComment($query->id,$commentresponse->comments[0]->id);
}
else{
$json->body=$a;
$queryIssue->updateComment($query->id,$commentresponse->comments[0]->id,$json);
}

//deleting from database

$sql="DELETE FROM trn_attachments
		WHERE id='".$DBID."' AND name='".$filename."'
	 ";
 mysql_query($sql);
		$query1->id= $jiraid;
		$array=$queryIssue->queryIssue($query1);
		$response1 = json_decode($array->responseBody);
		if(isset($response1->issues[0]->fields->description)){
			$sql1="SELECT * from trn_attachments where id=".$DBID;
			$sq=mysql_query($sql1);
		?>
		<div class="popattach">
			<p class="poptop">ATTACHMENTS :</p>
			
				<?while($row = mysql_fetch_array($sq)){?>
				<div class="popattachments"><a href="<?="http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/Attachments/".$row['name']?>" target="_blank"><?=$row['name']?></a></div>
				<div class="popdeleteattach"><a href="#" onclick="getattach('<?echo $row['name']?>','<?echo $DBID?>','<?echo $_GET['jiraid']?>');"><img src="images/icon_delete.gif"></a></div>
				<?}?>
			</div>
		<?}?>